//
//  WZGLoginPopVC.m
//  ZCJZ
//
//  Created by sujeking on 2020/2/20.
//  Copyright © 2020 jeking. All rights reserved.
//

#import "WZGLoginPopVC.h"
#import <FlexLib.h>
#import "SKBaseWebService.h"
@interface WZGLoginPopVC ()
@property (nonatomic, strong) UIView *agreeBtn;

@property (nonatomic, strong) CAGradientLayer *gLayer;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) FlexTextView *textView;


@property (nonatomic, copy) NSDictionary *dict;
@end

@implementation WZGLoginPopVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self netFetchProtoclData];
}

- (void)setupUI {
    self.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    CAGradientLayer *gLayer = [CAGradientLayer layer];
    gLayer.frame = self.agreeBtn.bounds;
    gLayer.colors = @[(__bridge id)UIColorFromRGB(0xFF533A).CGColor,
                      (__bridge id)UIColorFromRGB(0xF92924).CGColor,];
    gLayer.startPoint = CGPointMake(0, 0);
    gLayer.endPoint   = CGPointMake(1, 0);
    [self.agreeBtn.layer insertSublayer:gLayer atIndex:0];
}

- (void)agreeBtnClickAction {
    __weak typeof(self) weakSelf = self;
    [self dismissViewControllerAnimated:YES completion:^{
        weakSelf.btnClickCallback(YES);
    }];
}

- (void)disAgreeBtnClickAction {
    __weak typeof(self) weakSelf = self;
    [self dismissViewControllerAnimated:YES completion:^{
        weakSelf.btnClickCallback(NO);
    }];
}


- (void)setDict:(NSDictionary *)dict {
    _dict = dict;
    self.titleLabel.text = [dict valueForKey:@"protocol_name"];
    NSString *text = [dict valueForKey:@"protocol_content"];
    NSAttributedString *att = [[NSAttributedString alloc] initWithData:[text dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:nil error:nil];
    self.textView.attributedText = att;
}

// MARK: - NET

- (void)netFetchProtoclData {
    __weak typeof(self) weakSelf = self;
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:@"index_page" forKey:@"position"];
    [SKBaseWebService postRequest:@"/v1/protocol/get-protocol" parameters:parameters progress:nil success:^(ZCErrorCode status, NSString *msg, id data) {
        NSArray *arr = (NSArray *)data;
        weakSelf.dict = arr.firstObject;
    } failure:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        [weakSelf.view makeToast:msg duration:2.0 position:CSToastPositionCenter];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [weakSelf dismissViewControllerAnimated:YES completion:nil];
        });
    }];
    
}

@end
